<template>
  <div class="home">
    <div class="banner-box" ref="bannerbox">
      <img src="../../assets/images/home/youxin-background.png" alt="" />
      <div class="search-box">
        <div class="back-home" @click="FnHome">
          <img src="../../assets/images/home/left.png" alt="" />
          <div class="first-page">首页</div>
        </div>
        <div class="search">
          <input type="text" placeholder="品牌车系/车身类型/其他关键词" />
        </div>
        <div class="change-page">
          <img src="../../assets/images/home/change-page.png" alt="" />
        </div>
      </div>
    </div>
    <div
      class="car-List"
      ref="goodslist"
      @touchstart="FnStart($event)"
      @touchmove="FnMove"
      @touchend="FnEnd"
    >
      <div class="nav" ref="nav">
        <div class="nav-list ac">
          排序
          <img src="../../assets/images/home/oDown.png" alt="" />
        </div>
        <div class="nav-list">
          品牌
          <img src="../../assets/images/home/bDown.png" alt="" />
        </div>
        <div class="nav-list">
          价格
          <img src="../../assets/images/home/bDown.png" alt="" />
        </div>
        <div class="nav-list">
          车龄/里程
          <img src="../../assets/images/home/bDown.png" alt="" />
        </div>
        <div class="nav-list">
          配置/颜色
          <img src="../../assets/images/home/loudou.png" alt="" />
        </div>
      </div>
      <div class="goods-list">
        <div
          class="goods-content"
          v-for="item in CarList"
          :key="item.ID"
          @click="FnCarDetails"
        >
          <div class="goods-left">
            <img :src="`${imgState(item)}`" alt="" />
          </div>
          <div class="goods-right">
            <div class="grey-heart">
              <img src="../../assets/images/home/gray-heart.png" alt="" />
            </div>
            <div class="goods-title">
              {{ item.tit_con }}
            </div>
            <div class="goods-message">
              <div class="goods-new">85成新</div>
              <div class="goods-year">{{ item.buy_time }}</div>
              <div class="goods-kilometer">{{ item.mileage }}万公里</div>
            </div>
            <div class="goods-money">
              <div class="pay-money">
                {{ item.car_price }}
              </div>
              <div class="firstpay-money">首付{{ item.down_payments }}</div>
            </div>
            <div class="yx-serve">
              <div class="yx-icon">优信自营</div>
              <div class="back-car">7天无理由退车</div>
            </div>
          </div>
        </div>
        <div class="loading">Loading...</div>
      </div>
    </div>
    <div class="LoadHide" v-if="load">
      <img src="../../assets/images/home/home_loding.gif" alt="" />
    </div>
  </div>
</template>

<script>
import { getCarList } from "./../../api/index";
export default {
  data() {
    return {
      page: 1, //页码数
      CarList: [], //存放当前的数据
      CarCount: 0, //存放高度
      ImgList: [], //存放图片的数组
      ImageCount: 0, //用于记录图片的数量
      load: true, //loading加载
      count: 1,
      flag: true, //添加一个开关
      LoadSwitch: true, //接口开关
    };
  },
  methods: {
    // 跳转到首页
    FnHome() {
      this.$router.push("./");
    },
    // 跳转到详情页
    FnCarDetails() {
      this.$router.push("./CarDetails");
    },
    //获取汽车列表
    async FnGetCarList() {
      return await getCarList({ page: this.page });
    },
    //图片是否加载的判断
    imgState(item) {
      if (item.imgState) {
        return `http://www.ibugthree.com/${item.img_src}`;
      } else {
        return `http://www.ibugthree.com/default.gif`;
      }
    },
    // 图片预加载
    async FnInit() {
      await this.FnGetCarList();
      this.headerTop = this.$refs["bannerbox"].offsetHeight;
      // 获取可视区的高度
      this.viewHeight = document.documentElement.clientHeight;
      //获取整体的高度
      this.EveryHeight =
        this.$refs["goodslist"].getBoundingClientRect().height /
        this.CarList.length;

      // 设置高度
      this.FnSetHeight(1);
      // 图片预加载
      this.ImgList.push(`http://www.ibugthree.com/default.gif`);
      for (let i = 0; i < this.ImgList.length; i++) {
        let oImage = new Image();
        let URL = this.ImgList[i];
        oImage.src = URL;
        oImage.onload = () => {
          this.ImageCount++;
          if (this.ImageCount >= this.ImgList.length) {
            this.load = false;
            this.flag = true;
          }
        };
      }
    },
    // 设置高度
    FnSetHeight(whether) {
      for (let i = 0; i < this.CarList.length; i++) {
        let json = this.CarList[i];
        this.CarCount = this.EveryHeight * i;
        json.height = this.CarCount;
        if (this.CarCount > this.viewHeight) {
          json.imgState = 0;
        } else {
          json.imgState = 1;
        }
        // 判断是否要添加图片
        if (whether) {
          let URL = `http://www.ibugthree.com/${json.img_src}`;
          this.ImgList.push(URL);
        }
      }
    },
    // 状态初始化
    FnDate() {
      (this.page = 1), //页码数
        (this.CarList = []), //存放当前的数据
        (this.CarCount = 0), //存放高度
        (this.ImgList = []), //存放图片的数组
        (this.ImageCount = 0), //用于记录图片的数量
        (this.load = true), //loading加载
        (this.count = 1),
        (this.flag = true), //添加一个开关
        (this.LoadSwitch = true); //接口开关
    },
    // 上拉加载更多
    FnDelayMore() {
      // 滚动高度
      this.roll = document.documentElement.scrollTop || document.body.scrollTop;
      // 滚动高度+可视区的高度 = 大于页面的高度
      this.ScrollHeight = this.roll + this.viewHeight;

      for (var i = 0; i < this.CarList.length; i++) {
        let Car = this.CarList[i];
        if (this.ScrollHeight > Car.height) {
          Car.imgState = 1;
          this.$set(this.CarList, i, Car);
        }
      }
      //加载更多
      if (
        this.ScrollHeight >= this.$refs["goodslist"].scrollHeight &&
        this.flag
      ) {
        this.count = 1;
        this.flag = false;
        for (let i = 0; i < this.CarList.length; i++) {
          var Img = new Image();
          let URL = `http://www.ibugthree.com/${this.CarList[i].img_src}`; //图片地址
          Img.src = URL;
          Img.onload = async () => {
            this.count++;
            if (this.count >= this.CarList.length) {
              // 所有的数据设置高度
              this.FnSetHeight();
              this.flag = true;
              if (this.LoadSwitch) {
                this.LoadSwitch = false;
                this.page++;
                let CarData = await this.FnGetCarList();
                this.CarList = this.CarList.concat(CarData);
                this.LoadSwitch = true;
              }
            }
          };
        }
      }
    },
    //下拉刷新
    FnStart(ev) {
      this.$refs["goodslist"].classList.remove("active");
      this.startY = ev.changedTouches[0].pageY; //拖动的高度(鼠标距离顶部的距离)

      this.PageY =
        this.startY - this.$refs["goodslist"].getBoundingClientRect().top; //获取鼠标距离loadmore顶部的距离
    },
    FnMove(ev) {
      this.moveY = ev.changedTouches[0].pageY;
      this.roll = document.documentElement.scrollTop || document.body.scrollTop;
      if (this.moveY > this.startY && this.roll == 0) {
        this.Y = this.moveY - this.PageY; //向下移动的值
        this.scale = 1 - this.Y / 736;
        if (this.scale <= 0.54) {
          this.scale = 0.54;
        }
        this.$refs["goodslist"].style.transform = `translate(0,${
          this.Y * this.scale
        }px)`;
        // ev.preventDefault && ev.preventDefault();
      }
    },
    async FnEnd(ev) {
      this.UpY = ev.changedTouches[0].pageY;
      this.$refs["goodslist"].classList.add("active");
      this.$refs["goodslist"].style.transform = `translate(0,0)`;
      if (this.UpY > this.startY && this.roll == 0) {
        //所有数据恢复初始状态
        this.FnDate();
        this.page++;
        this.CarList = await this.FnGetCarList();
        //图片预加载 设置高度
        this.FnInit();
      }
    },
  },
  async mounted() {
    this.CarList = await this.FnGetCarList();
    this.FnInit();
    document.onscroll = this.FnDelayMore;
  },
};
</script>
<style lang="less" scoped>
/* // css初始化 */
body {
  margin: 0;
}

/* 去掉a的下划线 */
a {
  text-decoration: none;
}

/* 搜索框去除边框 */
input,
button {
  border: 0;
  /* 设置背景颜色为透明 */
  background-color: transparent;
  /* 去掉外轮廓 */
  outline: none;
}

/* 图片的空白间隙 */
img {
  vertical-align: middle;
}

/* // css初始化结束 */
.home {
  //过渡动画
  .active {
    transition: 0.15s ease-in all;
  }
  .banner-box {
    position: relative;
    width: 100%;
    height: 500px;
    img {
      width: 100%;
      height: 100%;
    }
    .search-box {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      height: 100px;
      width: 100%;
      .back-home {
        margin-left: 21px;
        display: flex;
        align-items: center;
        .first-page {
          font-size: 24px;
          color: #fff;
        }
        img {
          width: 21px;
          height: 40px;
          margin-right: 10px;
        }
      }
      .search {
        input {
          width: 460px;
          height: 64px;
          padding-left: 78px;
          line-height: 64px;
          background: rgba(255, 255, 255, 0.7)
            url("../../assets/images/home/search-list.png") no-repeat 30px
            center/31px 31px;
          border-radius: 80px;
        }
      }
      .change-page {
        margin-right: 21px;
        img {
          width: 48px;
          height: 39px;
        }
      }
    }
  }
  .car-List {
    background: #fff;
    z-index: 2;
    margin-top: -10px;
    border-top-right-radius: 12px;
    border-top-left-radius: 12px;
    .loading {
      width: 750px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      font-size: 30px;
      background: #fff;
      color: #ccc;
    }
    .nav {
      position: sticky;
      top: 0px;
      background: #fff;
      z-index: 999;
      display: flex;
      align-items: center;
      padding-left: 32px;
      height: 140px;
      .nav-list {
        display: flex;
        align-items: center;
        font-size: 24px;
        margin-right: 56px;
        &:nth-child(5) {
          margin-right: 0;
        }
        img {
          margin-left: 5px;
          width: 16px;
          height: 10px;
        }
        &:nth-child(5) {
          img {
            width: 20px;
            height: 20px;
          }
        }
      }
      .nav-list.ac {
        color: #ff5c03;
      }
    }
    .goods-content {
      display: flex;
      justify-content: space-between;
      padding: 0 16px 0 31px;
      margin-bottom: 84px;
      box-sizing: border-box;
      .goods-left {
        img {
          width: 276px;
          height: 206px;
        }
      }
      .goods-right {
        position: relative;
        width: 404px;
        .grey-heart {
          width: 34px;
          height: 30px;
          position: absolute;
          top: 115px;
          right: 31px;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .goods-title {
          font-size: 28px;
          font-weight: 700;
          margin-bottom: 15px;
        }
        .goods-message {
          display: flex;
          align-items: center;
          font-size: 22px;
          color: #bebebe;
          height: 22px;
        }
        .goods-new {
          width: 88px;
          height: 22px;
          line-height: 22px;
          border-right: 1px solid #909090;
        }
        .goods-year {
          width: 94px;
          height: 22px;
          line-height: 22px;
          border-right: 1px solid #909090;
          margin: 0 17px;
        }
        .goods-money {
          margin-bottom: 4px;
          display: flex;
          justify-content: start;
          align-items: baseline;
          .pay-money {
            display: flex;
            align-items: baseline;
            font-size: 24px;
            font-weight: 700;
            color: #e73400;

            div {
              font-size: 40px;
            }
            margin-right: 13px;
          }
          .firstpay-money {
            font-size: 24px;
            color: #949494;
          }
        }
        .yx-serve {
          display: flex;
          align-items: center;
          .yx-icon {
            padding: 3px;
            height: 26px;
            line-height: 26px;
            font-size: 17px;
            margin-right: 9px;
            border-radius: 5px;
            color: #723b00;
            background: #fadc8b;
          }
          .back-car {
            padding: 1px 4px;
            font-size: 18px;
            color: #8a8889;
            border-radius: 5px;
            border: 1px solid #e2e2e2;
          }
        }
      }
    }
  }
  // loading加载页
  .LoadHide {
    width: 100vw;
    height: 100vh;
    background: #fff;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    img {
      position: absolute;
      width: 200px;
      height: 200px;
      top: 50%;
      left: 50%;
      margin-top: -100px;
      margin-left: -100px;
    }
  }
}
</style>
